<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FriendController
 *
 * @author tuancasi
 */
class FriendController extends Controller {

    public function filters() {
	parent::filters();
	return array(
	    'accessControl'
	);
    }

    public function accessRules() {
	return array(
	    array('allow', // allow all users to perform 'index' and 'view' actions
		'actions' => array('list_member'),
		'users' => array('*'),
	    ),
	    array('allow',
		'actions' => array(
		    'find_friend', 'invite', 'add_friend', 'invite', 'index', 'incoming_request', 'pending_request', 'delete', 'delete_request'
		),
		'users' => array('@')
	    ),
	    array('deny', // deny everybody else
		'users' => array('*')
	    )
	);
    }

    /*
     * 
     */

    public function ActionFind_friend() {
	if (user()->id && user()->getModel()->status == User::STATUS_ACTIVE) {
	    $search = 0;
	    $model = new User('search');
	    $model->unsetAttributes();  // clear any default values
	    $model->type = '';
	    if (isset($_GET['User'])) {
		$model->attributes = $_GET['User'];
		$search = 1;
	    }
	    $this->render('find_friend', array(
		'model' => $model,
		'search' => $search
	    ));
	} else {
	    $this->redirect(SITE_NAME_URL);
	}
    }

    /**
     * invite a friend
     */
    public function actionInvite() {
	if (user()->id) {
	    if (isset($_POST['userReceive'])) {
		FriendRequest::insertRequest(user()->id, $_POST['userReceive']);
		echo 1;
	    }
	}
    }

    /**
     * add friend to friend list
     */
    public function actionAdd_friend() {
	if (isset($_POST['userId'])) {
	    //add friend
	    Friend::insertFriend(user()->id, $_POST['userId']);
	    Friend::insertFriend($_POST['userId'], user()->id);
	    //delete request
	    FriendRequest::deleteRequest($_POST['userId'], user()->id);
	}
    }

    /**
     * display all friend
     */
    public function actionIndex() {
	$model = new Friend('search');

	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['Friend'])) {
	    $model->attributes = $_GET['Friend'];
	}

	//update user is checked a new accepting friend
	Friend::updateChecked(user()->id);

	$this->render('index', array(
	    'model' => $model
	));
    }

    /**
     * incoming request
     */
    public function actionIncoming_request() {
	$model = new FriendRequest('search');

	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['FriendRequest'])) {
	    $model->attributes = $_GET['FriendRequest'];
	}

	//set have check inviting
	FriendRequest::updateChecked(user()->id);

	$this->render('incoming_request', array(
	    'model' => $model
	));
    }

    /**
     * pending request
     */
    public function actionPending_request() {

	$model = new FriendRequest('search');

	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['FriendRequest'])) {
	    $model->attributes = $_GET['FriendRequest'];
	}
	FriendRequest::updateCheckedPending(user()->id);
	$this->render('pending_request', array(
	    'model' => $model
	));
    }

    /**
     * delete a friend
     */
    public function actionDelete() {
	if (isset($_GET['id']) && $_GET['id'] != '') {
	    Friend::deleteFriend($_GET['id'], user()->id);
	}
    }

    /**
     * delete request
     */
    public function actionDelete_request() {

	if ((isset($_GET['sender']) && $_GET['sender'] != '') && (isset($_GET['receiver']) && $_GET['receiver'] != '')) {
	    FriendRequest::deleteRequest($_GET['sender'], $_GET['receiver']);
	}
    }

}

